home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / DefaultBoundedRangeModel.java < prev    next >
Text File  |  1998-06-30  |  10KB  |  348 lines

  1. /*
  2.  * @(#)DefaultBoundedRangeModel.java    1.25 98/02/05
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  */
  20.  
  21. package com.sun.java.swing;
  22.  
  23. import com.sun.java.swing.event.*;
  24.  
  25. import java.io.Serializable;
  26.  
  27.  
  28. /**
  29.  * A generic implementation of BoundedRangeModel.
  30.  * <p>
  31.  * Warning: serialized objects of this class will not be compatible with
  32.  * future swing releases.  The current serialization support is appropriate 
  33.  * for short term storage or RMI between Swing1.0 applications.  It will
  34.  * not be possible to load serialized Swing1.0 objects with future releases
  35.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  36.  * baseline for the serialized form of Swing objects.
  37.  *
  38.  * @version 1.25 02/05/98
  39.  * @author David Kloba
  40.  * @author Hans Muller
  41.  * @see BoundedRangeModel
  42.  */
  43. public class DefaultBoundedRangeModel implements BoundedRangeModel, Serializable
  44. {
  45.     /**
  46.      * Only one ChangeEvent is needed per model instance since the
  47.      * event's only (read-only) state is the source property.  The source
  48.      * of events generated here is always "this".
  49.      */
  50.     protected transient ChangeEvent changeEvent = null;
  51.  
  52.     protected EventListenerList listenerList = new EventListenerList();
  53.  
  54.     private int value = 0;
  55.     private int extent = 0;
  56.     private int min = 0;
  57.     private int max = 100;
  58.     private boolean isAdjusting = false;
  59.  
  60.  
  61.     /**
  62.      * Initializes all of the properties as follows:
  63.      * <ul>
  64.      * <li><code>value</code> = 0
  65.      * <li><code>extent</code> = 0
  66.      * <li><code>minimum</code> = 0
  67.      * <li><code>maximum</code> = 100
  68.      * <li><code>adjusting</code> = false
  69.      * </ul>
  70.      */
  71.     public DefaultBoundedRangeModel() {
  72.     }
  73.  
  74.  
  75.     /**
  76.      * Initializes value, extent, minimum and maximum.  Adjusting is false.
  77.      * Throws an IllegalArgumentException if the following constraints
  78.      * aren't satisfied:
  79.      * <pre>
  80.      * min <= value <= value+extent <= max
  81.      * </pre>
  82.      */
  83.     public DefaultBoundedRangeModel(int value, int extent, int min, int max)
  84.     {
  85.         if ((max >= min) && (value >= min) && ((value + extent) <= max)) {
  86.             this.value = value;
  87.             this.extent = extent;
  88.             this.min = min;
  89.             this.max = max;
  90.         }
  91.         else {
  92.             throw new IllegalArgumentException("invalid range properties");
  93.         }
  94.     }
  95.  
  96.  
  97.     /**
  98.      * @return the model's current value
  99.      * @see #setValue
  100.      * @see BoundedRangeModel#getValue
  101.      */
  102.     public int getValue() {
  103.       return value; 
  104.     }
  105.  
  106.  
  107.     /**
  108.      * @return the model's extent
  109.      * @see #setExtent
  110.      * @see BoundedRangeModel#getExtent
  111.      */
  112.     public int getExtent() {
  113.       return extent; 
  114.     }
  115.  
  116.  
  117.     /**
  118.      * @return the model's minimum
  119.      * @see #setMinimum
  120.      * @see BoundedRangeModel#getMinimum
  121.      */
  122.     public int getMinimum() {
  123.       return min; 
  124.     }
  125.  
  126.  
  127.     /**
  128.      * @return  the model's maximum
  129.      * @see #setMaximum
  130.      * @see BoundedRangeModel#getMaximum
  131.      */
  132.     public int getMaximum() {
  133.         return max; 
  134.     }
  135.  
  136.  
  137.     /** 
  138.      * Sets the value to <I>n</I> after ensuring that <I>n</I> falls
  139.      * within the model's constraints:
  140.      * <pre>
  141.      * minimum <= value <= value+extent <= maximum
  142.      * </pre>
  143.      * 
  144.      * @see BoundedRangeModel#setValue
  145.      */
  146.     public void setValue(int n) {
  147.         int newValue = Math.max(n, min);
  148.         if(newValue + extent > max) {
  149.             newValue = max - extent; 
  150.         }
  151.         setRangeProperties(newValue, extent, min, max, isAdjusting);
  152.     }
  153.  
  154.  
  155.     /** 
  156.      * Sets the extent to <I>n</I> after ensuring that <I>n</I> 
  157.      * is greater than or equal to zero and falls within the model's 
  158.      * constraints:
  159.      * <pre>
  160.      * minimum <= value <= value+extent <= maximum
  161.      * </pre>
  162.      * 
  163.      * @see #getExtent
  164.      * @see BoundedRangeModel#setExtent
  165.      */
  166.     public void setExtent(int n) {
  167.         int newExtent = Math.max(0, n);
  168.         if(value + newExtent > max) {
  169.             newExtent = max - value;
  170.         }
  171.         setRangeProperties(value, newExtent, min, max, isAdjusting);
  172.     }
  173.  
  174.  
  175.     /** 
  176.      * Sets the minimum to <I>n</I> after ensuring that <I>n</I> 
  177.      * that the other three properties obey the model's constraints:
  178.      * <pre>
  179.      * minimum <= value <= value+extent <= maximum
  180.      * </pre>
  181.      * 
  182.      * @see #getMinimum
  183.      * @see BoundedRangeModel#setMinimum
  184.      */
  185.     public void setMinimum(int n) {
  186.         int newMax = Math.max(n, max);
  187.         int newValue = Math.max(n, value);
  188.         int newExtent = Math.min(newMax - newValue, extent);
  189.         setRangeProperties(newValue, newExtent, n, newMax, isAdjusting);
  190.     }
  191.  
  192.  
  193.     /** 
  194.      * Sets the maximum to <I>n</I> after ensuring that <I>n</I> 
  195.      * that the other three properties obey the model's constraints:
  196.      * <pre>
  197.      * minimum <= value <= value+extent <= maximum
  198.      * </pre>
  199.      * 
  200.      * @see #getMaximum
  201.      * @see BoundedRangeModel#setMaximum
  202.      */
  203.     public void setMaximum(int n) {
  204.         int newMin = Math.min(n, min);
  205.         int newValue = Math.min(n, value);
  206.         int newExtent = Math.min(n - newValue, extent);
  207.  
  208.         setRangeProperties(newValue, newExtent, newMin, n, isAdjusting);
  209.     }
  210.  
  211.  
  212.     /**
  213.      * Sets the valueIsAdjusting property.
  214.      * 
  215.      * @see #getValueIsAdjusting
  216.      * @see #setValue
  217.      * @see BoundedRangeModel#setValueIsAdjusting
  218.      */
  219.     public void setValueIsAdjusting(boolean b) {
  220.         setRangeProperties(value, extent, min, max, b);
  221.     }
  222.  
  223.  
  224.     /**
  225.      * Returns true if the value is in the process of changing
  226.      * as a result of actions being taken by the user.
  227.      *
  228.      * @return the value of the valueIsAdjusting property
  229.      * @see #setValueIsAdjusting
  230.      * @see #setValue
  231.      * @see BoundedRangeModel#getValueIsAdjusting
  232.      */
  233.     public boolean getValueIsAdjusting() {
  234.         return isAdjusting; 
  235.     }
  236.  
  237.  
  238.     /**
  239.      * Sets all of the BoundedRangeModel properties after forcing
  240.      * the arguments to obey the usual constraints:
  241.      * <pre>
  242.      * minimum <= value <= value+extent <= maximum
  243.      * </pre>
  244.      * <p>
  245.      * At most, one ChangeEvent is generated.
  246.      * 
  247.      * @see BoundedRangeModel#setRangeProperties
  248.      * @see #setValue
  249.      * @see #setExtent
  250.      * @see #setMinimum
  251.      * @see #setMaximum
  252.      * @see #setValueIsAdjusting
  253.      */
  254.     public void setRangeProperties(int newValue, int newExtent, int newMin, int newMax, boolean adjusting)
  255.     {
  256.         if(newMin > newMax)
  257.             newMin = newMax;
  258.         if(newValue > newMax)
  259.             newMax = newValue;
  260.         if(newValue < newMin)
  261.             newMin = newValue;
  262.         if((newExtent + newValue) > newMax)
  263.             newExtent = newMax - newValue;
  264.         if(newExtent < 0)
  265.             newExtent = 0;
  266.  
  267.         boolean isChange =
  268.             (newValue != value) ||
  269.             (newExtent != extent) ||
  270.             (newMin != min) ||
  271.             (newMax != max) ||
  272.             (adjusting != isAdjusting);
  273.  
  274.         if (isChange) {
  275.             value = newValue;
  276.             extent = newExtent;
  277.             min = newMin;
  278.             max = newMax;
  279.             isAdjusting = adjusting;
  280.  
  281.             fireStateChanged();
  282.         }
  283.     }
  284.  
  285.  
  286.     /**
  287.      * Adds a ChangeListener.  The change listeners are run each
  288.      * time any one of the Bounded Range model properties changes.
  289.      *
  290.      * @param l the ChangeListener to add
  291.      * @see #removeChangeListener
  292.      * @see BoundedRangeModel#addChangeListener
  293.      */
  294.     public void addChangeListener(ChangeListener l) {
  295.         listenerList.add(ChangeListener.class, l);
  296.     }
  297.     
  298.  
  299.     /**
  300.      * Removes a ChangeListener.
  301.      *
  302.      * @param l the ChangeListener to remove
  303.      * @see #addChangeListener
  304.      * @see BoundedRangeModel#removeChangeListener
  305.      */
  306.     public void removeChangeListener(ChangeListener l) {
  307.         listenerList.remove(ChangeListener.class, l);
  308.     }
  309.  
  310.  
  311.     /** 
  312.      * Run each ChangeListeners stateChanged() method.
  313.      * 
  314.      * @see #setRangeProperties
  315.      * @see EventListenerList
  316.      */
  317.     protected void fireStateChanged() 
  318.     {
  319.         Object[] listeners = listenerList.getListenerList();
  320.         for (int i = listeners.length - 2; i >= 0; i -=2 ) {
  321.             if (listeners[i] == ChangeListener.class) {
  322.                 if (changeEvent == null) {
  323.                     changeEvent = new ChangeEvent(this);
  324.                 }
  325.                 ((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
  326.             }          
  327.         }
  328.     }   
  329.  
  330.     
  331.     /**
  332.      * Returns a string that displays all of the BoundedRangeModel properties.
  333.      */
  334.     public String toString()  {
  335.         String modelString =
  336.             "value=" + getValue() + ", " +
  337.             "extent=" + getExtent() + ", " +
  338.             "min=" + getMinimum() + ", " +
  339.             "max=" + getMaximum() + ", " +
  340.             "adj=" + getValueIsAdjusting();
  341.  
  342.         return getClass().getName() + "[" + modelString + "]";
  343.     }
  344.  
  345.  
  346. }
  347.  
  348.